# coding: utf-8
from PySide6.QtCore import Qt
from PySide6.QtGui import QColor
from PySide6.QtWidgets import QHBoxLayout, QVBoxLayout, QSizePolicy
from qfluentwidgets import SimpleCardWidget, IconWidget, BodyLabel, setFont

from ...common import APIBase
from ...components import ImageLabel


class PluginCardWidget(SimpleCardWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.horizontalLayout = QHBoxLayout(self)
        self.verticalLayout = QVBoxLayout()
        self.iconWidget = IconWidget(self)
        self.titleLabel = BodyLabel(self)
        self.versionLabel = BodyLabel(self)
        self.contentLabel = BodyLabel(self)

        self.iconWidget.setFixedSize(80, 80)
        self.titleLabel.setWordWrap(True)
        self.contentLabel.setWordWrap(True)
        self.contentLabel.setTextFormat(Qt.TextFormat.RichText)
        self.setMinimumSize(300, 120)
        self.setFixedHeight(120)
        setFont(self.titleLabel, 16)
        setFont(self.versionLabel, 12)
        setFont(self.contentLabel, 12)
        self.versionLabel.setTextColor(QColor(0, 0, 0, 150), QColor(245, 245, 245, 150))
        self.contentLabel.setTextColor(QColor(0, 0, 0, 150), QColor(245, 245, 245, 150))

        self.titleLabel.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)
        self.contentLabel.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)

        self.horizontalLayout.setContentsMargins(20, 10, 20, 10)
        self.horizontalLayout.setSpacing(20)
        self.horizontalLayout.setAlignment(Qt.AlignmentFlag.AlignLeft)
        self.verticalLayout.setAlignment(Qt.AlignmentFlag.AlignVCenter)
        self.verticalLayout.setSpacing(0)
        self.horizontalLayout.addWidget(self.iconWidget)
        self.verticalLayout.addStretch(1)
        self.verticalLayout.addWidget(self.titleLabel, 1)
        self.verticalLayout.addWidget(self.versionLabel, 0)
        self.verticalLayout.addWidget(self.contentLabel, 1)
        self.verticalLayout.addStretch(1)
        self.horizontalLayout.addLayout(self.verticalLayout)

    def setPlugin(self, plugin: APIBase):
        self.iconWidget.setIcon(plugin.icon)
        self.titleLabel.setText(plugin.name)
        self.versionLabel.setText(plugin.version)
        description = plugin.description
        if len(description) > 30:
            description = description[:30] + "..."
        self.contentLabel.setText(description)

        self.contentLabel.adjustSize()
